package com.blog.admin.controller;

import com.blog.admin.vo.PageVO;
import com.blog.common.vo.JsonResult;
import com.blog.user.service.ConsumerService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/rest/admin/usermanager")
public class UserController {
    private static final Log log = LogFactory.getLog(UserController.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private ConsumerService consumerService;

    @PostMapping("/getList")
    public JsonResult userList(@RequestBody String json, HttpServletRequest request){
        JsonResult result = JsonResult.fail();
        try {
            JsonNode jsonNode = objectMapper.readTree(json);
            JsonNode pageJson = jsonNode.get("pageInfo");
            JsonNode paramsJson = jsonNode.get("params");
            PageVO pageVO = objectMapper.readValue(pageJson.toPrettyString(), PageVO.class);
            Map<String,String> params = objectMapper.readValue(paramsJson.toPrettyString(), Map.class);
            result = consumerService.getConsumerList(pageVO, params);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            log.error("",e);
        }
        return result;
    }
}
